package com.batdream.message.service.impl;

import com.batdream.message.enums.MsgSignFlagEnum;
import com.batdream.message.mapper.ChatMsgByNettyMapper;
import com.batdream.message.mapper.ChatMsgMapper;
import com.batdream.message.pojo.ChatMsgByNetty;
import com.batdream.message.pojo.ChatMsg;
import com.batdream.message.pojo.Users;
import com.batdream.message.service.ChatService;
import com.batdream.message.utils.MD5Utils;
import com.batdream.message.utils.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author Wang Chen Peng
 * @date 2018/12/30
 * describe:
 */
@Service
public class ChatServiceImpl implements ChatService {

    private final ChatMsgMapper chatMsgMapper;
    private final ChatMsgByNettyMapper chatMsgByNettyMapper;

    @Autowired
    public ChatServiceImpl(ChatMsgMapper chatMsgMapper, ChatMsgByNettyMapper chatMsgByNettyMapper) {
        this.chatMsgMapper = chatMsgMapper;
        this.chatMsgByNettyMapper = chatMsgByNettyMapper;
    }

    @Override
    public String saveMsg(ChatMsgByNetty chatMsg) {
        ChatMsg chatMsgDb = new ChatMsg();
        chatMsgDb.setId(Sid.next());
        chatMsgDb.setAcceptUserId(chatMsg.getReceiverId());
        chatMsgDb.setCreateTime(new Date());
        chatMsgDb.setMsg(chatMsg.getMsg());
        chatMsgDb.setSignFlag(MsgSignFlagEnum.unsign.getType());
        chatMsgDb.setSendUserId(chatMsg.getSenderId());
        chatMsgMapper.insert(chatMsgDb);
        return chatMsgDb.getId();
    }

    @Override
    public void updateMsgToSigend(List<String> idsList) {
        chatMsgMapper.updateMsgToSigend(idsList);
    }

    @Override
    public List<ChatMsg> getNoneReadMsgByUserId(String myUserId) {

        //SELECT * FROM chat_msg WHERE accept_user_id = '1901039BRB6MC5GC' AND sign_flag='0'
        Example userExample = new Example(ChatMsg.class);
        Example.Criteria criteria = userExample.createCriteria();
        criteria.andEqualTo("acceptUserId", myUserId);
        criteria.andEqualTo("signFlag", 0);
        return chatMsgMapper.selectByExample(userExample);
    }

    @Override
    public Collection<ChatMsg> getNoneReadChatSnapshot(String myUserId) {
        List<ChatMsg> chatMsgs = chatMsgMapper.getNoneReadChatSnapshot(myUserId);
        ConcurrentHashMap<String, ChatMsg> result = new ConcurrentHashMap<>();
        chatMsgs.stream().parallel().forEach(chatMsg -> {
            if (result.containsKey(chatMsg.getSendUserId())) {
                if (result.get(chatMsg.getSendUserId()).getCreateTime().getTime() < chatMsg.getCreateTime().getTime()) {
                    result.put(chatMsg.getSendUserId(), chatMsg);
                }
            } else {
                result.put(chatMsg.getSendUserId(), chatMsg);
            }
        });

        return result.values();
    }
}
